package classes.managers {
	import classes.controlers.Jump_Rabbits_Controler;
	import classes.controlers.Rope_Controler;
	import classes.controlers.Rope_Rabbits_Controler;
	import classes.global.Global_Properties;
	import classes.scenes.A_Scene;
	import classes.sounds.SoundCollection;
	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Game_Manager extends A_Scene {
		private var cnt:int = 0;
		private var rabbitCnt:int = 0;
		private var spaceBarCnt:int = 0;
		private var comboCnt:int = 0;
		
		private var score:int = 0;
		
		private var actTime:uint;
		
		private var rope:Rope_Controler;
		private var ropeRabbits:Rope_Rabbits_Controler;
		private var jumpRabbits:Jump_Rabbits_Controler;
		private var startTime:Number;
		private var endTime:Number;
		private var speedBonusOldY:Number;
		private var comboBonusOldY:Number;
		
		private var rabbitArr:Array;
		private var comboBonus:Boolean;
		private var comboStart:Boolean = false;
		private var success:Boolean = false;
		private var k:int;
		
		public function Game_Manager( _main:Main, _mc:MovieClip ) {
			super( _main, _mc );
			
			rabbitArr = [ mc.rabbit1_mc, mc.rabbit2_mc, mc.rabbit3_mc, mc.rabbit4_mc, mc.rabbit5_mc, mc.rabbit6_mc, mc.rabbit7_mc ];
			
			rope = new Rope_Controler( this, mc.rope_mc );
			ropeRabbits = new Rope_Rabbits_Controler( this, mc.ropeRabbit_mc );
			jumpRabbits = new Jump_Rabbits_Controler( this, rabbitArr );
			
			rope.init();
			jumpRabbits.init();
		}
		
		private function KEY_UP(e:KeyboardEvent):void {
			if ( e.keyCode == Keyboard.SPACE ) {
				++spaceBarCnt;
				//trace("space : ", spaceBarCnt)
				jumpRabbits.readyRabbits( spaceBarCnt ); // 뛸 준비하는 토끼.
				
				mc.spaceBar_mc.gotoAndStop(1);
				
				SoundCollection.spaceClickSound();
				
				if ( rabbitCnt === spaceBarCnt ) {
					endTime = getTimer();
					
					actTime = endTime - startTime;
					mc.actTime_mc.actTime_txt.text = String( (actTime/1000).toFixed(3) );
					mc.actTime_mc.visible = true;
					mc.actTime_mc.gotoAndPlay(2);
					
					success = true;
					//scoreCounting();
					
					spaceBarCnt = 0;
					if ( main.stage.hasEventListener(KeyboardEvent.KEY_UP) ) {
						main.stage.removeEventListener(KeyboardEvent.KEY_UP, KEY_UP );
						main.stage.removeEventListener(KeyboardEvent.KEY_DOWN, KEY_DOWN );
					}
				}
			}
		}
		
		private function KEY_DOWN(e:KeyboardEvent):void {
			if ( e.keyCode == Keyboard.SPACE ) {
				spaceBarArrow( false );
				mc.spaceBar_mc.gotoAndStop(2);
			}
		}
		
		public function init():void {
			cnt = 0;
			rabbitCnt = 0;
			spaceBarCnt = 0;
			score = 0;
			comboCnt = 0;
			comboStart = false;
			success = false;
			
			speedBonusOldY = mc.bonus_mc.y;
			comboBonusOldY = mc.comboBonus_mc.y
			
			mc.actTime_mc.visible = false;
			mc.bonus_mc.visible = false;
			mc.comboBonus_mc.visible = false;
			mc.rabbitNum_mc.gotoAndStop( rabbitCnt + 1 );
			mc.spaceBar_mc.gotoAndStop(1);
			//counting();
			
			successNumInit( mc.count_mc )
			scoreInit( mc.score_mc );
			numberBoard( 0, mc.bonus_mc );
			numberBoard( 0, mc.comboBonus_mc );
			
			spaceBarArrow( false );
			
			swapInit();
			rope.init();
			jumpRabbits.init();
			rope.twist();
		}
		
		public function upRope_START():void {
			cnt++;
			if ( cnt === 1 ) {  // 차례에 맞춰서 줄에 들어오는 토끼.
				jumpRabbits.enterRabbits(); 
			}else if ( cnt === 5 ) {
				jumpRabbits.enterRabbits();
			}else if ( cnt === 8 ) {
				jumpRabbits.enterRabbits();
			}else if ( cnt === 9 ) {
				jumpRabbits.enterRabbits();
			}else if ( cnt === 10 ) {
				jumpRabbits.enterRabbits();
			}else if ( cnt === 11 ) {
				jumpRabbits.enterRabbits();
			}else if ( cnt === 12 ) {
				jumpRabbits.enterRabbits();
			}else if ( cnt === 13 ) {
				comboStart = true;
			}
			
			main.stage.addEventListener(KeyboardEvent.KEY_UP, KEY_UP );
			main.stage.addEventListener(KeyboardEvent.KEY_DOWN, KEY_DOWN );
			
			startTime = getTimer();
		}
		
		private function spaceBarArrow( _flag:Boolean ):void {
			if ( _flag ) mc.spaceBar_mc.arrow_mc.visible = true;
			else mc.spaceBar_mc.arrow_mc.visible = false;
		}
		
		public function upRope_END():void {
			if ( main.stage.hasEventListener(KeyboardEvent.KEY_UP) ) {
				main.stage.removeEventListener(KeyboardEvent.KEY_UP, KEY_UP );
				main.stage.removeEventListener(KeyboardEvent.KEY_DOWN, KEY_DOWN );
			}
			
			//SoundCollection.ropeSound();
		}
		
		public function downRope_START():void {
			jumpRabbits.fallRabbits(rabbitCnt); //줄에걸리는 토끼
			
			if ( cnt === 1 ) {
				rabbitCnt++; 
			}else if ( cnt === 5 ) {
				rabbitCnt++; 
			}else if ( cnt === 8 ) {
				rabbitCnt++; 
			}else if ( cnt === 9 ) {
				rabbitCnt++; 
			}else if ( cnt === 10 ) {
				rabbitCnt++;
			}else if ( cnt === 11 ) {
				rabbitCnt++; 
			}else if ( cnt === 12 ) {
				rabbitCnt++; 
			}else if ( cnt > 12 ) {
				
			}
			
			if ( rabbitCnt === 7 ) {
				if ( comboStart ) {
					if ( 1 > actTime / 1000 ) { // 스페이스바 누른 타이밍이 1초보다 낮을때만 콤보.
						comboCnt++;
						
						if ( comboCnt === 1 ) {
							SoundCollection.combo1Sound();
						}else if ( comboCnt === 2 ) {
							SoundCollection.combo2Sound();
						}else if ( comboCnt === 3 ) {
							SoundCollection.combo3Sound();
						}else if ( comboCnt >= 4 ) {
							SoundCollection.combo4Sound();
						}
						
						
						mc.combo_mc.combo_mc.gotoAndStop( comboCnt );
						mc.combo_mc.gotoAndPlay(2);
						
						if ( comboCnt === 7 ) {
							comboCnt = 0;
							comboBonus = true;
						}
					}else {
						comboCnt = 0;
					}
				}
			}
			
			if ( success ) scoreCounting();
			
			
			
			
			mc.actTime_mc.visible = false;
			mc.actTime_mc.gotoAndStop(1);
			
			rope.ropeSpeed( rabbitCnt ); // 줄넘기 줄 속도 조절.
			ropeRabbits.twist( rabbitCnt );
			trace("twist")
			jumpRabbits.jumpRabbits();  //점프하는 토끼.
		}
		
		public function downRope_END():void {
			if ( cnt === 1 ) {
				jumpRabbits.arrowMark(1);  // 다음차례 토끼 머리위 화살표
			}
			
			if ( cnt >= 1 ) {
				spaceBarArrow( true );
				//SoundCollection.spaceStartSound();
			}
			
			mc.rabbitNum_mc.gotoAndStop( rabbitCnt + 1 );
			spaceBarCnt = 0;  // 스페이스바 누른 횟수 초기화.
			jumpRabbits.nomalRabbits( rabbitCnt );  // 대기상태 토끼.
			jumpRabbits.arrowMark( rabbitCnt );  // 다음차례 토끼 머리위 화살표
			
			counting();
		}
		
		// 점수 계산.
		private function scoreCounting():void {
			var j:Number = Number((actTime / 1000).toFixed(3));
			var h:Number = rope.time;
			
			switch (rope.UP_ROPE_SPEED) {
				case "속도1": k = 70;
				break;
				case "속도2": k = 75;
				break;
				case "속도3": k = 80;
				break;
				case "속도4": k = 85;
				break;
				case "속도5": k = 90;
				break;
				case "속도6": k = 95;
				break;
				case "속도7": k = 100;
				break;
				case "속도8": k = 105;
				break;
				case "속도9": k = 110;
				break;
				case "속도10": k = 120;
				break;
				default:
			}
			
			if ( j >= 1 ) {
				score += k;
			}else {
				if ( rabbitCnt >= 7 ) {
					var bonus:Number = int(( 1 - j ) * 100000 / k);
					mc.bonus_mc.visible = true;
					numberBoard( bonus, mc.bonus_mc );
					tweenSpeedBonus();
					score += (k + bonus);
					
					SoundCollection.speedBonusSound();
					
					if ( comboBonus ) { //7콤보 보너스
						score += k; 
						mc.comboBonus_mc.visible = true;
						
						tweenComboBonus();
						comboBonus = false;
						SoundCollection.combo7Sound();
					}
					
				}else {
					k = 60;
					score += k;
				}
			}
			numberBoard( score, mc.score_mc );
		}
		
		private function tweenSpeedBonus():void {
			TweenMax.to( mc.bonus_mc, 1, { y:speedBonusOldY - 40, ease:Quad.easeOut, onComplete:tweenSpeedComplete } );
		} private function tweenSpeedComplete():void {
			TweenMax.to( mc.bonus_mc, .5, { y:mc.bonus_mc.y - 80, alpha:0.3,  onComplete:tweenComplete2 } );
		} private function tweenComplete2():void {
			mc.bonus_mc.visible = false;
			mc.bonus_mc.y = speedBonusOldY;
			mc.bonus_mc.alpha = 1;
		}
		
		private function tweenComboBonus():void {
			TweenMax.to( mc.comboBonus_mc, 1, { y:comboBonusOldY - 40, ease:Quad.easeOut, onComplete:tweenComboComplete } );
		} private function tweenComboComplete():void {
			TweenMax.to( mc.comboBonus_mc, .5, { y:mc.comboBonus_mc.y - 80, alpha:0.3, onComplete:tweenComboComplete2 } );
		} private function tweenComboComplete2():void {
			numberBoard( k, mc.comboBonus_mc );
			mc.comboBonus_mc.visible = false;
			mc.comboBonus_mc.y = comboBonusOldY;
			mc.comboBonus_mc.alpha = 1;
		}
		
		//줄넘은 횟수 카운팅.
		private function counting():void {
			numberBoard( cnt, mc.count_mc );
		}
		
		private function numberBoard( _cnt:Number, _mc:MovieClip ):void {
			var str:String;
			var num10000:String;
			var num1000:String;
			var num100:String;
			var num10:String;
			var num1:String;
			
			/*if ( _cnt === 0 ) {
				 _mc.num10000_mc.gotoAndStop(1);
				 _mc.num1000_mc.gotoAndStop(1);
				 _mc.num100_mc.gotoAndStop(1);
				 _mc.num10_mc.gotoAndStop(1);
				 _mc.num1_mc.gotoAndStop(1);
			}
			*/
			if ( _cnt < 10 ) {
				_mc.num1_mc.gotoAndStop( _cnt +1 );
			}else if ( _cnt >= 10 && _cnt < 100 ) {
				str = String( _cnt );
				num10 = str.substr(0, 1);
				num1 = str.substr(1, 1);
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 100 && _cnt < 1000 ) {
				str = String( _cnt );
				num100 = str.substr(0, 1);
				num10 = str.substr(1, 1);
				num1 = str.substr(2, 1);
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 1000 && _cnt < 10000 ) {
				str = String( _cnt );
				num1000 = str.substr(0, 1);
				num100 = str.substr(1, 1);
				num10 = str.substr(2, 1);
				num1 = str.substr(3, 1);
				
				if( Number(num1000) === 0 ) _mc.num1000_mc.gotoAndStop( 1 );
				else _mc.num1000_mc.gotoAndStop( Number(num1000) + 1 );
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}else if ( _cnt >= 10000 && _cnt < 100000 ) {
				str = String( _cnt );
				num10000 = str.substr(0, 1);
				num1000 = str.substr(1, 1);
				num100 = str.substr(2, 1);
				num10 = str.substr(3, 1);
				num1 = str.substr(4, 1);
				
				if( Number(num10000) === 0 ) _mc.num10000_mc.gotoAndStop( 1 );
				else _mc.num10000_mc.gotoAndStop( Number(num10000) + 1 );
				
				if( Number(num1000) === 0 ) _mc.num1000_mc.gotoAndStop( 1 );
				else _mc.num1000_mc.gotoAndStop( Number(num1000) + 1 );
				
				if( Number(num100) === 0 ) _mc.num100_mc.gotoAndStop( 1 );
				else _mc.num100_mc.gotoAndStop( Number(num100) + 1 );
				
				if( Number(num10) === 0 ) _mc.num10_mc.gotoAndStop( 1 );
				else _mc.num10_mc.gotoAndStop( Number(num10)+1 );
				
				if( Number(num1) === 0 ) _mc.num1_mc.gotoAndStop( 1 );
				else _mc.num1_mc.gotoAndStop( Number(num1)+1 );
			}
		}
		
		
		private function successNumInit( _mc:MovieClip):void {
			 _mc.num100_mc.gotoAndStop(1);
			 _mc.num10_mc.gotoAndStop(1);
			 _mc.num1_mc.gotoAndStop(1);
		}
		
		private function scoreInit( _mc:MovieClip):void {
			_mc.num10000_mc.gotoAndStop(1);
			 _mc.num1000_mc.gotoAndStop(1);
			 _mc.num100_mc.gotoAndStop(1);
			 _mc.num10_mc.gotoAndStop(1);
			 _mc.num1_mc.gotoAndStop(1);
		}
		
		public function gameOver():void {
			trace("gamgOver");
			Global_Properties.score = score;
			Global_Properties.cnt = cnt;
			
			//Global_Properties.score = 293955500;
			
			rope.stop();
			main.gameOverInit();
		}
		
		public function swapInit():void {
			var i:int;
			var length:int = rabbitArr.length;
			for ( i = 0; i < length; ++i ) {
				mc.setChildIndex( rabbitArr[i], mc.getChildIndex(mc.rope_mc) - 1);
			}
		}
		
		public function swapRope1():void {
			jumpRabbits.swapRope1( mc, mc.rope_mc );
		}
		public function swapRope2():void {
			jumpRabbits.swapRope2( mc, mc.rope_mc );
		}
	}
}